package org.osprey.apidoc.service.impl;

import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import org.osprey.apidoc.auth.JwtProvider;
import org.osprey.apidoc.common.BaseServiceImpl;
import org.osprey.apidoc.pojo.User;
import org.osprey.apidoc.service.UserService;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName UserServiceImpl
 * @Description:
 * @Author Osprey
 * @Date 2019/10/16
 * @Version V2.0
 **/
public class UserServiceImpl  extends BaseServiceImpl implements UserService {
    private Vertx vertx;
    public UserServiceImpl(Vertx vertx, JsonObject config) {
        super(vertx, config);
        this.vertx = vertx;
    }

    @Override
    public Integer addUser(User paramUser) {
        return null;
    }

    @Override
    public UserService getUserByUserName(String userName, Handler<AsyncResult<User>> resultHandler) {
        retrieveOne(userName, "select * from api_user where userName = ?").map(option -> option.map(User :: new).orElse(null)).setHandler(resultHandler);
        return this;
    }

    @Override
    public UserService login(String userName, String password, Handler<AsyncResult<Map<String, Object>>> resultHandler) {
        retrieveOne(userName, "select * from api_user where userName = ?").map(option -> option.map(User :: new).orElse(null)).setHandler(loginHandle -> {
            Map<String, Object> resultMap = new HashMap<>();
            if (loginHandle.succeeded()) {
                User user = loginHandle.result();
                if (this.getMd5(password).equals(user.getUserPassword())) {
                    user.setUserPassword("");
                    resultMap.put("loginUser", user);
                    String token  = JwtProvider.generateToken(vertx, new JsonObject().put("userId", user.getUserID()).put("userName", userName));
                    resultMap.put("message", "用户登录成功");
                    resultMap.put("token", token);
                } else {
                    resultMap.put("message", "用户名或密码错误");
                }
            } else {
                resultMap.put("message", "用户不存在");
            }
            Future<Map<String, Object>> future = Future.future();
            future.complete(resultMap);
            resultHandler.handle(future);
        });
        return this;
    }


    @Override
    public boolean changePassword(String userName, String oldPassword, String newPassword) {
        return false;
    }

    @Override
    public boolean changeNickName(Integer userId, String nickName) {
        return false;
    }

    @Override
    public Map<String, Object> getUserInfo(Integer userId) {
        return null;
    }

    private String getMd5(String str) {
        try {
            MessageDigest  md = MessageDigest.getInstance("MD5");
            md.update(str.getBytes());
            String md5 = (new BigInteger(1, md.digest())).toString(16);
            return fillMD5(md5);
        } catch (NoSuchAlgorithmException e) {
           throw  new RuntimeException("MD5加密错误");
        }
    }

    public static String fillMD5(String md5) { return (md5.length() == 32) ? md5 : fillMD5("0" + md5); }
}
